<?php



class PlaceCitiesController extends AppController{
    public $uses = array ('PlaceCountry','PlaceDepartment','PlaceCity');
    
     public $layout = 'place';
     
    public function searchCity(){        
        $listCountries = $this->PlaceCountry->find('list',array(
            'recursive' => -1
        ));
        
        if (!empty($this->request->data)){
            $data = $this->request->data;            
            $search = $data['Search'];
            $searchResults = array();
            
            
            if (!empty($search['city'])){
                $conditions['PlaceCity.name LIKE'] = '%'.$search['city'].'%';
                
                $searchResults['City'] = $this->PlaceCity->find('all', array(
                    'conditions' => $conditions,
                    'recursive' => 0
                ));
                
            }
            
            $this->set('searchResults', $searchResults);
        }
        
        $this->set('listCountries', $listCountries);
    }
    
    
    public function view($id){
        $city = $this->PlaceCity->find('first', array(
            'conditions' => array(
                'PlaceCity.places_id' => $id,
            ),
            'recursive' => 0
        ));
        
        $groups = $this->PlaceCity->Group->find('all', array(
            'conditions' => array(
                'Group.status' => 'active',
                'Group.cities_id' => $id,
            ),
            'recursive' => 1
        ));
        
        $propositionsGroups = $this->PlaceCity->Group->find('all', array(
            'conditions' => array(
                'Group.status' => 'proposition',
                'Group.cities_id' => $id,
            ),
            'recursive' => 1
        ));
        
        
        $this->set('city', $city);
        $this->set('groups', $groups);
        $this->set('propositionsGroups', $propositionsGroups);
    }
    
    
    public function home(){
        $data = $this->getCurrentUserGroup();
        $this->redirect(array('controller' => 'Places', 'action' => 'view', $data['Group']['cities_id']));
    }
    
    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow( 'view');
    }
    
}


?>
